<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>饲养列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/default/easyui.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/icon.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/easyui/css/demo.css}">
    <script type="text/javascript" th:src="@{/easyui/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/easyui/jquery.easyui.min.js}"></script>
    <script type="text/javascript" th:src="@{/easyui/js/validateExtends.js}"></script>
    <script type="text/javascript" th:inline="javascript">
        /*<![CDATA[*/
        $(function() {
            //datagrid初始化
            $('#dataList').datagrid({
                title:'饲养列表',
                iconCls:'icon-more',//图标
                border: true,
                collapsible: false,//是否可折叠的
                fit: true,//自动大小
                method: "post",
                url:[[@{/feed/getFeedList}]],
            idField:'id',
                singleSelect: false,//是否单选
                pagination: true,//分页控件
                rownumbers: true,//行号
                sortName:'id',
                sortOrder:'DESC',
                remoteSort: false,
                columns: [
                [
                    {field:'chk',checkbox: true,width:50},
                    {field:'id',title:'ID',width:50},
                    {field:'cowId',title:'牛编号',width:200,
                        formatter: function(value,row,index){
                            if (row.cowId){
                                var cowList = $("#cowList").combobox("getData");
                                for(var i=0;i<cowList.length;i++ ){
                                    //console.log(clazzList[i]);
                                    if(row.cowId == cowList[i].id)return cowList[i].sn;
                                }
                                return row.cowId;
                            } else {
                                return 'not found';
                            }
                        }
                    },
                    {field:'foodId',title:'食物名称',width:200,
                        formatter: function(value,row,index){
                            if (row.foodId){
                                var foodList = $("#foodList").combobox("getData");
                                for(var i=0;i<foodList.length;i++ ){
                                    //console.log(clazzList[i]);
                                    if(row.foodId == foodList[i].id)return foodList[i].name;
                                }
                                return row.foodId;
                            } else {
                                return 'not found';
                            }
                        }
                    },
                    {field:'feedWeight',title:'饲喂量',width:200},
                    {field:'breederId',title:'饲养员',width:200,
                        formatter: function(value,row,index){
                            if (row.breederId){
                                var breederList = $("#breederList").combobox("getData");
                                for(var i=0;i<breederList.length;i++ ){
                                    //console.log(clazzList[i]);
                                    if(row.breederId == breederList[i].id)return breederList[i].username;
                                }
                                return row.breederId;
                            } else {
                                return 'not found';
                            }
                        }
                    },
                    {field:'remark',title:'备注',width:200}
                ]
            ],
                toolbar: "#toolbar",
                onBeforeLoad : function(){
                try{
                    $("#cowList,#add_cowList,#edit_cowList").combobox("getData");
                    $("#foodList,#add_foodList,#edit_foodList").combobox("getData");
                    $("#breederList,#add_breederWeight,#edit_breederList").combobox("getData");
                }catch(err){
                    preLoadClazz();
                }
            }
        });
            //提前加载信息
            function preLoadClazz(){
                $("#cowList,#add_cowList,#edit_cowList").combobox({
                    width: "150",
                    height: "25",
                    valueField: "id",
                    textField: "sn",
                    multiple: false, //可多选
                    editable: false, //不可编辑
                    method: "post",
                    url: [[@{/cow/getCowList(from=combox)}]],

            });
                $("#foodList,#add_foodList,#edit_foodList").combobox({
                    width: "150",
                    height: "25",
                    valueField: "id",
                    textField: "name",
                    multiple: false, //可多选
                    editable: false, //不可编辑
                    method: "post",
                    url: [[@{/food/getFoodList(from=combox)}]],

            });
                $("#breederList,#add_breederList,#edit_breederList").combobox({
                    width: "150",
                    height: "25",
                    valueField: "id",
                    textField: "username",
                    multiple: false, //可多选
                    editable: false, //不可编辑
                    method: "post",
                    url: [[@{/breeder/getBreederList(from=combox)}]],

            });
            }

            //设置分页控件
            var p = $('#dataList').datagrid('getPager');
            $(p).pagination({
                pageSize: 10,//每页显示的记录条数，默认为10
                pageList: [10,20,30,50,100],//可以设置每页记录条数的列表
                beforePageText: '第',//页数文本框前显示的汉字
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });

            //设置工具类按钮
            $("#add").click(function(){
                $("#addDialog").dialog("open");
            });

            //设置导入工具类按钮
            $("#import").click(function(){
                $("#importDialog").dialog("open");
            });

            //设置编辑按钮
            $("#edit").click(function(){
                table = $("#editTable");
                var selectRows = $("#dataList").datagrid("getSelections");
                if(selectRows.length != 1){
                    $.messager.alert("消息提醒", "请选择一条数据进行操作!", "warning");
                } else{
                    $("#edit_id").val(selectRows[0].id);
                    $("#editDialog").dialog("open");
                }
            });


            //删除
            $("#delete").click(function(){
                var selectRow = $("#dataList").datagrid("getSelected");
                if(selectRow == null){
                    $.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
                } else{
                    var id = selectRow.id;
                    $.messager.confirm("消息提醒", "确定删除饲喂记录吗，确认继续？", function(r){
                        if(r){
                            $.ajax({
                                type: "post",
                                url: [[@{/feed/deleteFeed}]],
                            data: {id: id},
                            success: function(data){
                                if(data.success){
                                    $.messager.alert("消息提醒",data.message,"info");
                                    //刷新表格
                                    $("#dataList").datagrid("reload");
                                    $("#dataList").datagrid("uncheckAll");
                                }else{
                                    $.messager.alert("消息提醒",data.message,"warning");
                                    return;
                                }
                            }
                        });
                        }
                    });
                }
            });

            //设置添加窗口
            $("#addDialog").dialog({
                title: "添加饲喂信息",
                width: 450,
                height: 450,
                iconCls: "icon-add",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text:'添加',
                        plain: true,
                        iconCls:'icon-book-add',
                        handler:function(){
                            var validate = $("#addForm").form("validate");
                            if(!validate){
                                $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                                return;
                            } else{
                                $.ajax({
                                    type: "post",
                                    url: [[@{/feed/addFeed}]],
                                data: $("#addForm").serialize(),
                                    success: function(data){
                                    if(data.success){
                                        $.messager.alert("消息提醒",data.message,"info");
                                        //关闭窗口
                                        $("#addDialog").dialog("close");
                                        //清空原表格数据
                                        $("#add_remark").textbox('setValue', "");
                                        //刷新
                                        $('#dataList').datagrid("reload");
                                    } else{
                                        $.messager.alert("消息提醒",data.message,"warning");
                                        return;
                                    }
                                }
                            });
                            }
                        }
                    },
                    {
                        text:'重置',
                        plain: true,
                        iconCls:'icon-book-reset',
                        handler:function(){
                            $("#add_remark").textbox('setValue', "");
                            $("#add_feedWeight").textbox('setValue', "");
                        }
                    },
                ]
            });

            //设置修改窗口
            $("#editDialog").dialog({
                title: "修改饲喂信息",
                width: 450,
                height: 450,
                iconCls: "icon-edit",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text:'修改',
                        plain: true,
                        iconCls:'icon-book-add',
                        handler:function(){
                            var validate = $("#editForm").form("validate");
                            if(!validate){
                                $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                                return;
                            } else{
                                $.ajax({
                                    type: "post",
                                    url: [[@{/feed/editFeed}]],
                                data: $("#editForm").serialize(),
                                    success: function(data){
                                    if(data.success){
                                        $.messager.alert("消息提醒",data.message,"info");
                                        //关闭窗口
                                        $("#editDialog").dialog("close");
                                        //清空原表格数据
                                        $("#edit_remark").textbox('setValue', "");
                                        //刷新
                                        $('#dataList').datagrid("reload");
                                    }else{
                                        $.messager.alert("消息提醒",data.message,"warning");
                                        return;
                                    }
                                }
                            });
                            }
                        }
                    },
                    {
                        text:'重置',
                        plain: true,
                        iconCls:'icon-book-reset',
                        handler:function(){
                            $("#edit_remark").textbox('setValue', "");
                            $("#edit_feedWeight").textbox('setValue', "");
                        }
                    },
                ],
                onBeforeOpen: function(){
                    var selectRow = $("#dataList").datagrid("getSelected");
                    //设置值
                    $("#edit_cowList").combobox('setValue', selectRow.cowId);
                    $("#edit_feedWeight").numberbox('setValue', selectRow.feedWeight);
                    $("#edit_remark").textbox('setValue', selectRow.remark);
                    $("#edit_foodList").combobox('setValue', selectRow.foodId);
                    $("#edit_breederList").combobox('setValue', selectRow.breederId);


                }
            });

            //设置导入窗口
            $("#importDialog").dialog({
                title: "导入饲喂信息",
                width: 450,
                height: 150,
                iconCls: "icon-add",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text:'确认导入',
                        plain: true,
                        iconCls:'icon-book-add',
                        handler:function(){
                            var validate = $("#importForm").form("validate");
                            if(!validate){
                                $.messager.alert("消息提醒","请选择文件!","warning");
                                return;
                            } else{
                                importFeed();
                                $("#importDialog").dialog("close");
                            }
                        }
                    }
                ]
            });

            //下拉框通用属性
            $("#add_foodList,#foodList,#edit_foodList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "name",
                multiple: false, //不可多选
                editable: false, //不可编辑
                method: "post",
            });
            $("#add_cowList,#cowList,#edit_cowList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "sn",
                multiple: false, //不可多选
                editable: false, //不可编辑
                method: "post",
            });
            $("#add_breederList,#breederList,#edit_breederList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "username",
                multiple: false, //不可多选
                editable: false, //不可编辑
                method: "post",
            });
            //添加信息学生选择框
            $("#add_cowList").combobox({
                url: [[@{/cow/getCowList(from=combox)}]],
        });


            //修改信息学生选择框
            $("#edit_cowList").combobox({
                url: [[@{/cow/getCowList(from=combox)}]],

        });

            //搜索按钮监听事件
            $("#search-btn").click(function(){
                $('#dataList').datagrid('load',{
                    cowid: $("#cowList").combobox('getValue') == '' ? 0 : $("#cowList").combobox('getValue'),
                    foodid: $("#foodList").combobox('getValue') == '' ? 0 : $("#foodList").combobox('getValue'),
                    breederid: $("#breederList").combobox('getValue') == '' ? 0 : $("#breederList").combobox('getValue')
                });
            });

            //导出按钮监听事件
            $("#export").click(function(){
                cowid = $("#cowList").combobox('getValue') == '' ? 0 : $("#cowList").combobox('getValue');
                foodid = $("#foodList").combobox('getValue') == '' ? 0 : $("#foodList").combobox('getValue');
                breederid =  $("#breederList").combobox('getValue') == '' ? 0 : $("#breederList").combobox('getValue');
                url = [[@{/feed/exportFeed}]] + "?cowId=" + cowid + "&foodId="+foodid + "&breederId="+breederid;
                window.location.href = url;
            });

            //清空搜索条件
            $("#clear-btn").click(function(){
                $('#dataList').datagrid("reload",{});
                $("#cowList").combobox('clear');
                $("#foodList").combobox('clear');
                $("#breederList").combobox('clear');
            });

            function importFeed(){
                $("#importForm").submit();
                $.messager.progress({text:'正在上传导入中...'});
                var interval = setInterval(function(){
                    var message =  $(window.frames["import_target"].document).find("#message").text();
                    if(message != null && message != ''){
                        $.messager.progress('close');
                        $.messager.alert("消息提醒",message,"info");
                        $('#dataList').datagrid("reload");
                        clearInterval(interval);
                    }
                }, 1000)
            }
        });
        /*]]>*/
    </script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">

</table>
<!-- 工具栏 -->
<div id="toolbar">
    <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="import" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">导入</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="export" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">导出</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="edit" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left; margin-right: 10px;"><a id="delete" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="margin-top: 3px;">
        牛编号：<input id="cowList" class="easyui-textbox" name="cowList" />
        食物名称：<input id="foodList" class="easyui-textbox" name="foodList" />
        饲养员：<input id="breederList" class="easyui-textbox" name="breederList" />
        <a id="search-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
        <a id="clear-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">清空搜索</a>
    </div>
</div>

<!-- 添加数据窗口 -->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8" >
            <tr>
                <td style="width:40px">牛编号:</td>
                <td colspan="3">
                    <input id="add_cowList" style="width: 200px; height: 30px;" class="easyui-textbox" name="cowId" data-options="required:true, missingMessage:'请选择牛编号'" />
                </td>
                <td style="width:80px"></td>
            </tr>
            <tr>
                <td style="width:40px">食物名称:</td>
                <td colspan="3">
                    <input id="add_foodList" style="width: 200px; height: 30px;" class="easyui-textbox" name="foodId" data-options="required:true, missingMessage:'请选择食物名称'" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td style="width:40px">饲喂量:</td>
                <td colspan="3">
                    <input id="add_feedWeight" style="width: 200px; height: 30px;" class="easyui-numberbox" data-options="required:true,min:0,precision:2, missingMessage:'请填写正确的饲喂量'" name="feedWeight" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td style="width:40px">饲养员名称:</td>
                <td colspan="3">
                    <input id="add_breederList" style="width: 200px; height: 30px;" class="easyui-textbox" name="breederId" data-options="required:true, missingMessage:'请选择饲养员'" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td>备注:</td>
                <td>
                    <textarea id="add_remark" name="remark" style="width: 300px; height: 160px;" class="easyui-textbox" data-options="multiline:true" ></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- 修改数据窗口 -->
<div id="editDialog" style="padding: 10px">
    <form id="editForm" method="post">

        <input type="hidden" id="edit_id" name="id">
        <table cellpadding="8" >
            <tr>
                <td style="width:40px">牛编号:</td>
                <td colspan="3">
                    <input id="edit_cowList" style="width: 200px; height: 30px;" class="easyui-textbox" name="cowId" data-options="required:true, missingMessage:'请选择牛编号'"/>
                </td>
                <td style="width:80px"></td>
            </tr>
            <tr>
                <td style="width:40px">食物名称:</td>
                <td colspan="3">
                    <input id="edit_foodList" style="width: 200px; height: 30px;" class="easyui-textbox" name="foodId" data-options="required:true, missingMessage:'请选择食物'" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td style="width:40px">饲喂量:</td>
                <td colspan="3">
                    <input id="edit_feedWeight" style="width: 200px; height: 30px;" class="easyui-numberbox" data-options="required:true,min:0,precision:2, missingMessage:'请填写正确的饲喂量'" name="feedWeight" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td style="width:40px">饲养员:</td>
                <td colspan="3">
                    <input id="edit_breederList" style="width: 200px; height: 30px;" class="easyui-textbox" name="breederId" data-options="required:true, missingMessage:'请选择饲养员'" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td>备注:</td>
                <td>
                    <textarea id="edit_remark" name="remark" style="width: 300px; height: 160px;" class="easyui-textbox" data-options="multiline:true" ></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- 导入数据窗口 -->
<div id="importDialog" style="padding: 10px">
    <form id="importForm" method="post" enctype="multipart/form-data" th:action="@{/feed/importFeed}" target="import_target">
        <table cellpadding="8" >
            <tr>
                <td>请选择文件:</td>
                <td>
                    <input class="easyui-filebox" name="importFeed" data-options="required:true,min:0,precision:2, missingMessage:'请选择文件',prompt:'选择文件'" style="width:200px;">
                </td>
            </tr>

        </table>
    </form>
</div>
<!-- 提交表单处理iframe框架 -->
<iframe id="import_target" name="import_target"></iframe>
</body>
</html>